set(MOBILITYDB_MODULE_PATHNAME "$libdir/lib${MOBILITYDB_LIB_NAME}")
set(extschema "@extschema@")

configure_file(mobilitydb.control ${CMAKE_BINARY_DIR}/mobilitydb.control)

macro(process_file f srcdir bindir)
  file(READ ${srcdir}/${f}.in.sql CURR_CONTENTS)

  if (${POSTGRESQL_VERSION_NUMBER} GREATER_EQUAL 130000)
    string(REPLACE "#if POSTGRESQL_VERSION_NUMBER >= 130000" "-- if POSTGRESQL_VERSION_NUMBER >= 130000" CURR_CONTENTS "${CURR_CONTENTS}")
    string(REPLACE "#endif //POSTGRESQL_VERSION_NUMBER >= 130000" "-- endif POSTGRESQL_VERSION_NUMBER >= 130000" CURR_CONTENTS "${CURR_CONTENTS}")

    string(REPLACE "#if POSTGRESQL_VERSION_NUMBER < 130000" "/* --if POSTGRESQL_VERSION_NUMBER < 130000" CURR_CONTENTS "${CURR_CONTENTS}")
    string(REPLACE "#endif //POSTGRESQL_VERSION_NUMBER < 130000" "-- endif POSTGRESQL_VERSION_NUMBER < 130000 */" CURR_CONTENTS "${CURR_CONTENTS}")
  else()
    string(REPLACE "#if POSTGRESQL_VERSION_NUMBER >= 130000" "/* -- if POSTGRESQL_VERSION_NUMBER >= 130000" CURR_CONTENTS "${CURR_CONTENTS}")
    string(REPLACE "#endif //POSTGRESQL_VERSION_NUMBER >= 130000" "-- endif POSTGRESQL_VERSION_NUMBER >= 130000 */" CURR_CONTENTS "${CURR_CONTENTS}")

    string(REPLACE "#if POSTGRESQL_VERSION_NUMBER < 130000" " -- if POSTGRESQL_VERSION_NUMBER >= 130000" CURR_CONTENTS "${CURR_CONTENTS}")
    string(REPLACE "#endif //POSTGRESQL_VERSION_NUMBER < 130000" "-- endif POSTGRESQL_VERSION_NUMBER >= 130000" CURR_CONTENTS "${CURR_CONTENTS}")
  endif()

  if (${POSTGRESQL_VERSION_NUMBER} GREATER_EQUAL 140000)
    string(REPLACE "#if POSTGRESQL_VERSION_NUMBER >= 140000" "-- if POSTGRESQL_VERSION_NUMBER >= 140000" CURR_CONTENTS "${CURR_CONTENTS}")
    string(REPLACE "#endif //POSTGRESQL_VERSION_NUMBER >= 140000" "-- endif POSTGRESQL_VERSION_NUMBER >= 140000" CURR_CONTENTS "${CURR_CONTENTS}")

    string(REPLACE "#if POSTGRESQL_VERSION_NUMBER < 140000" "/* --if POSTGRESQL_VERSION_NUMBER < 140000" CURR_CONTENTS "${CURR_CONTENTS}")
    string(REPLACE "#endif //POSTGRESQL_VERSION_NUMBER < 140000" "-- endif POSTGRESQL_VERSION_NUMBER < 140000 */" CURR_CONTENTS "${CURR_CONTENTS}")
  else()
    string(REPLACE "#if POSTGRESQL_VERSION_NUMBER >= 140000" "/* -- if POSTGRESQL_VERSION_NUMBER >= 140000" CURR_CONTENTS "${CURR_CONTENTS}")
    string(REPLACE "#endif //POSTGRESQL_VERSION_NUMBER >= 140000" "-- endif POSTGRESQL_VERSION_NUMBER >= 140000 */" CURR_CONTENTS "${CURR_CONTENTS}")

    string(REPLACE "#if POSTGRESQL_VERSION_NUMBER < 140000" " -- if POSTGRESQL_VERSION_NUMBER >= 140000" CURR_CONTENTS "${CURR_CONTENTS}")
    string(REPLACE "#endif //POSTGRESQL_VERSION_NUMBER < 140000" "-- endif POSTGRESQL_VERSION_NUMBER >= 140000" CURR_CONTENTS "${CURR_CONTENTS}")
  endif()

  if (${POSTGRESQL_VERSION_NUMBER} GREATER_EQUAL 160000)
    string(REPLACE "#if POSTGRESQL_VERSION_NUMBER >= 160000" "-- if POSTGRESQL_VERSION_NUMBER >= 160000" CURR_CONTENTS "${CURR_CONTENTS}")
    string(REPLACE "#endif //POSTGRESQL_VERSION_NUMBER >= 160000" "-- endif POSTGRESQL_VERSION_NUMBER >= 160000" CURR_CONTENTS "${CURR_CONTENTS}")

    string(REPLACE "#if POSTGRESQL_VERSION_NUMBER < 160000" "/* --if POSTGRESQL_VERSION_NUMBER < 160000" CURR_CONTENTS "${CURR_CONTENTS}")
    string(REPLACE "#endif //POSTGRESQL_VERSION_NUMBER < 160000" "-- endif POSTGRESQL_VERSION_NUMBER < 160000 */" CURR_CONTENTS "${CURR_CONTENTS}")
  else()
    string(REPLACE "#if POSTGRESQL_VERSION_NUMBER >= 160000" "/* -- if POSTGRESQL_VERSION_NUMBER >= 160000" CURR_CONTENTS "${CURR_CONTENTS}")
    string(REPLACE "#endif //POSTGRESQL_VERSION_NUMBER >= 160000" "-- endif POSTGRESQL_VERSION_NUMBER >= 160000 */" CURR_CONTENTS "${CURR_CONTENTS}")

    string(REPLACE "#if POSTGRESQL_VERSION_NUMBER < 160000" " -- if POSTGRESQL_VERSION_NUMBER >= 160000" CURR_CONTENTS "${CURR_CONTENTS}")
    string(REPLACE "#endif //POSTGRESQL_VERSION_NUMBER < 160000" "-- endif POSTGRESQL_VERSION_NUMBER >= 160000" CURR_CONTENTS "${CURR_CONTENTS}")
  endif()

  file(WRITE ${bindir}/${f}.sql.in "${CURR_CONTENTS}")
endmacro()

add_subdirectory(general)
add_subdirectory(point)
if(NPOINT)
  add_subdirectory(npoint)
endif()
if(POSE)
  add_subdirectory(pose)
endif()

list(SORT PROJECT_SQL_FILES)

add_custom_target(all_sql_files ALL
  DEPENDS ${PROJECT_SQL_FILES})

if (PROJECT_VERBOSE)
  message(STATUS "PROJECT_SQL_FILES=${PROJECT_SQL_FILES}")
endif()

foreach (f ${PROJECT_SQL_FILES})
  file(READ ${f} CURR_CONTENTS)
  file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/${MOBILITYDB_EXTENSION_FILE}.in "${CURR_CONTENTS}")
endforeach()

configure_file(${CMAKE_CURRENT_BINARY_DIR}/${MOBILITYDB_EXTENSION_FILE}.in ${CMAKE_BINARY_DIR}/${MOBILITYDB_EXTENSION_FILE} COPYONLY)

# Set the library name extension according to the platform
if(WIN32)
  set(LIB_NAME_EXT ".dll")
else()
  set(LIB_NAME_EXT ".so")
endif()
message(STATUS "Library name extension: '${LIB_NAME_EXT}'")

file(READ "${CMAKE_BINARY_DIR}/${MOBILITYDB_EXTENSION_FILE}" sql_commands)
string(REPLACE "MODULE_PATHNAME" "${CMAKE_BINARY_DIR}/lib${MOBILITYDB_LIB_NAME}${LIB_NAME_EXT}" sql_commands "${sql_commands}")
message(STATUS "Substitution: ${CMAKE_BINARY_DIR}/lib${MOBILITYDB_LIB_NAME}${LIB_NAME_EXT}")
string(REPLACE "@extschema@" "public" sql_commands "${sql_commands}")
